package bid

import (
	"strings"
	"wx-api/adx-bid/cache"
	"wx-api/adx-bid/model/m_dsp_adv_creative"
	"wx-api/adx-bid/model/m_dsp_adv_plan"
	"wx-api/adx-bid/models/bid/BidModel"
	"wx-api/adx-bid/utils"
)

func DoAdm(req *BidModel.BidRequest, bid *BidModel.BidResponse_SeatBid_Bid, plan_info *m_dsp_adv_plan.DspAdvPlan, ad_info *m_dsp_adv_creative.DspAdvCreative) {
	imp := req.GetImp()[0]
	https := false
	if imp.GetSecure() == 1 {
		https = true
	}
	is_sc := DspRtbAdSeachPlanAdImp(imp)
	img_arr := ad_info.ImgAdm
	if is_sc == 2 {
		img_arr = ad_info.VideoAdm
	} else if is_sc == 0 {
		img_arr = append(img_arr, ad_info.VideoAdm...)
	}
	img_arr_num := utils.RandInt64(0, int64(len(img_arr)-1))
	adm := img_arr[img_arr_num]
	title_num := utils.RandInt64(0, int64(len(ad_info.Title)-1))
	title := ad_info.Title[title_num]

	bid.Impid = imp.GetId()
	bid.Price = float32(ad_info.Price * 100)
	bid.Adid = ad_info.PlanId
	bid.Crid = ad_info.Code
	bid.Title = title
	bid.Desc = adm.Desc
	bid.Source = "hy"
	bid.ClickUrl = DoAdmCrHttps(ad_info.Link, https)
	if req.GetDevice().GetOs() == "ios" {
		bid.Dpurl = ad_info.UnLink
	} else {
		bid.Dpurl = ad_info.DpLink
	}
	if ad_info.AppId != "" {
		app_info, ok := cache.CacheDspAdvAppMapGet(ad_info.AppId)
		if ok {
			bid.App = &BidModel.BidResponse_SeatBid_Bid_App{
				Name: app_info.Name,
				Pkg:  app_info.Pkg,
				Icon: app_info.Icon,
				Size: int32(app_info.Size),
				Ver:  app_info.Ver,
				Dev:  app_info.Dev,
				Pri:  app_info.Pri,
				Per:  app_info.Per,
				Furl: app_info.Furl,
				Fstr: app_info.Fstr,
			}
		}
	}

	if adm.AdmType == "1" { //图片
		if imp.AdType == "1" && imp.GetNative() != nil && imp.GetNative().GetINums() > 1 {
			if len(ad_info.ImgAdm) > int(imp.GetNative().GetINums()) {
				img_adm := utils.RandomSampleInt(len(ad_info.ImgAdm), int(imp.GetNative().GetINums()))
				for _, i2 := range img_adm {
					tmp_adm := ad_info.ImgAdm[i2]
					bid.Imgs = append(bid.Imgs, &BidModel.BidResponse_SeatBid_Bid_Image{
						Url:  DoAdmCrHttps(tmp_adm.AdUrls, https),
						W:    int32(tmp_adm.Width),
						H:    int32(tmp_adm.Height),
						Desc: tmp_adm.Desc,
					})
					bid.W = int32(tmp_adm.Width)
					bid.H = int32(tmp_adm.Height)
				}
			} else {
				for _, creativeAdm := range ad_info.ImgAdm {
					bid.Imgs = append(bid.Imgs, &BidModel.BidResponse_SeatBid_Bid_Image{
						Url:  DoAdmCrHttps(creativeAdm.AdUrls, https),
						W:    int32(creativeAdm.Width),
						H:    int32(creativeAdm.Height),
						Desc: creativeAdm.Desc,
					})
					bid.W = int32(creativeAdm.Width)
					bid.H = int32(creativeAdm.Height)
				}
			}
		} else {
			bid.Imgs = []*BidModel.BidResponse_SeatBid_Bid_Image{
				{
					Url:  DoAdmCrHttps(adm.AdUrls, https),
					W:    int32(adm.Width),
					H:    int32(adm.Height),
					Desc: adm.Desc,
				},
			}
			bid.W = int32(adm.Width)
			bid.H = int32(adm.Height)
		}
	}
	if adm.AdmType == "2" { //视频
		bid.W = int32(adm.Width)
		bid.H = int32(adm.Height)
		bid.Video = &BidModel.BidResponse_SeatBid_Bid_Video{
			Url:      DoAdmCrHttps(adm.AdUrls, https),
			W:        int32(adm.Width),
			H:        int32(adm.Height),
			Duration: int32(adm.Duration),
			Cimg: func(a string) *BidModel.BidResponse_SeatBid_Bid_Image {
				if a == "" {
					return nil
				}
				return &BidModel.BidResponse_SeatBid_Bid_Image{
					Url: DoAdmCrHttps(a, https),
					W:   int32(adm.Width),
					H:   int32(adm.Height),
				}
			}(adm.CImg),
			Size: adm.Size,
			Ext: &BidModel.BidResponse_SeatBid_Bid_Video_VideoExt{
				Start: func(a string) []string {
					if a != "" {
						return []string{a}
					}
					return nil
				}(plan_info.TracksVdStart),
				End: func(a string) []string {
					if a != "" {
						return []string{a}
					}
					return nil
				}(plan_info.TracksVdEnd),
			},
		}
	}
	//监测地址
	if plan_info.TracksPv != "" {
		bid.Views = append(bid.Views, plan_info.TracksPv)
	}
	if plan_info.TracksClick != "" {
		bid.Clicks = append(bid.Clicks, plan_info.TracksClick)
	}
	if plan_info.TracksDpSc != "" {
		bid.DplinksSuccess = append(bid.DplinksSuccess, plan_info.TracksDpSc)
	}
}

func DoAdmCrHttps(a string, ishttps bool) string {
	if ishttps {
		return strings.ReplaceAll(a, "http://", "https://")
	}
	return a
}
